package com.guan.cmsservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guan.cmsservice.entity.CrmBanner;
import com.guan.cmsservice.service.CrmBannerService;
import com.guan.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-03-13
 *
 * 管理员使用
 */
@RestController
@RequestMapping("/cmsservice/bannerAdmin/*")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService bannerService;

    /**
     * @date: 2023/3/13 12:24
     * @author: 管奇安
     * @description: 分页查询banner
     * @param: [page, limit]
     * @return: com.guan.commonutils.R
     **/
    @GetMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page, @PathVariable long limit){

        Page<CrmBanner> pageBanner = new Page<>(page,limit);
        IPage<CrmBanner> banners = bannerService.page(pageBanner, null);
        return R.ok()
                .data("items",banners.getRecords())
                .data("total",banners.getTotal());
    }

    /**
     * @date: 2023/3/13 12:25
     * @author: 管奇安
     * @description: 添加banner
     * @param: [crmBanner]
     * @return: com.guan.commonutils.R
     **/
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        bannerService.save(crmBanner);
        return R.ok().message("添加Banner成功");
    }

    /**
     * @date: 2023/3/13 12:25
     * @author: 管奇安
     * @description: 通过bannerid查询
     * @param: [bannerId]
     * @return: com.guan.commonutils.R
     **/
    @GetMapping("getBannerById/{bannerId}")
    public R getBannerById(@PathVariable String bannerId){
        CrmBanner banner = bannerService.getById(bannerId);
        return R.ok().data("item",banner);
    }

    /**
     * @date: 2023/3/13 12:25
     * @author: 管奇安
     * @description: 通过 bannerid 修改
     * @param: [crmBanner]
     * @return: com.guan.commonutils.R
     **/
    @PutMapping("updateBannerById")
    public R updateBannerById(@RequestBody CrmBanner crmBanner){
        bannerService.updateById(crmBanner);
        return R.ok().message("修改Banner成功");
    }

    /**
     * @date: 2023/3/13 12:26
     * @author: 管奇安
     * @description: 删除banner
     * @param: [bannerId]
     * @return: com.guan.commonutils.R
     **/
    @DeleteMapping("deleteBannerById/{bannerId}")
    public R deleteBannerById(@PathVariable String bannerId){
        bannerService.removeById(bannerId);
        return R.ok().message("删除Banner成功");
    }

}

